<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" layout:decorate="main"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
    <title>用户添加</title>
    <link th:href="@{/localcss/crmlist.css}" href="../static/localcss/crmadd.css" rel="stylesheet">
</head>
<body>
<div layout:fragment="content">
    <div class="">
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>客户信息 <i class="fa fa-user"></i></h2>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                        <div class="clearfix"></div>
                        <form class="form-horizontal form-label-left" th:action="@{/customer/save}" action="/customer/save" method="post">
                            <input type="hidden" name="custNo" th:value="${customer.custNo}" id="custNo">
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="custName"> 公司 <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input th:value="${customer.custName}" id="custName" class="form-control col-md-7 col-xs-12"
                                           data-validate-length-range="10" data-validate-words="1" name="custName" required="required"
                                           placeholder="请输入公司" type="text">
                                </div>
                                <span id="name"></span>
                            </div>
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="custRegion"> 地区 <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <select name="custRegion" id="custRegion" class="form-control" required="required">
                                        <option th:each="addre:${address}" th:selected="${customer.custRegion eq addre.dictValue}" th:value="${addre.dictValue}" th:text="${addre.dictItem}" value="">地区</option>
                                    </select>
                                </div>
                            </div>
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12"  for="custManagerId"> 负责人 <span class="required">*</span></label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="hidden" name="custManagerName" th:value="${customer.custManagerName}" id="custManagerName">
                                    <select name="custManagerId" id="custManagerId" class="form-control" required="required">
                                        <option th:each="user:${users}" th:selected="${customer.custManagerId eq user.usrId}" th:value="${user.usrId}" th:text="${user.usrName}" value="">负责人</option>
                                    </select>
                                </div>
                            </div>
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12"  for="custLevel"> 客户等级 <span class="required">*</span></label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="hidden" name="custLevelLabel" th:value="${customer.custLevelLabel}" id="custLevelLabel">
                                    <select name="custLevel" id="custLevel" class="form-control" required="required">
                                        <option th:each="dict:${dicts}" th:selected="${#strings.toString(customer.custLevel) eq dict.dictValue}" th:value="${dict.dictValue}" th:text="${dict.dictItem}" value="">客户等级</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-3">
                                    <button id="send" type="submit" class="btn btn-success">保存</button>
                                    <button type="button" class="btn btn-primary" id="back">返回</button>
                                    <br/><br/>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script layout:fragment="js">
    $(document).ready(function(){
        $("#back").on("click",function(){
            window.history.back();
        });
        $("#custManagerName").val($("#custManagerId").find('option:selected').text())
        $("#custLevelLabel").val($("#custLevel").find('option:selected').text())
    });

    $("#custManagerId").change(function (){
        $("#custManagerName").val($(this).find('option:selected').text());
    })
    $("#custLevel").change(function (){
        $("#custLevelLabel").val($(this).find('option:selected').text());
    })

    $("#custName").on('change', function (){
        var custName = $(this).val(); // 获取input的值并存储在变量中
        if (custName.trim() !== '') { // 确保值不为空
            $.ajax({
                type:"GET",
                url:"/customer/name/" + encodeURIComponent(custName), // 使用encodeURIComponent来确保URL正确编码
                dataType:"json",
                success:function(data){
                    if(data === true){ // 假设服务器返回的是一个对象，并且包含一个名为"available"的属性
                        alert("名称可用");
                    }else{
                        alert("名称不可用");
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    // 错误处理时最好显示具体的错误信息，而不是简单的“对不起，删除失败！”
                    alert("发生错误: " + textStatus + " - " + errorThrown);
                }
            });
        }
    });
</script>
</html>